package vistas;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.table.DefaultTableModel;

import clases.Roles_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import java.awt.Cursor;
import javax.swing.JScrollPane;
import java.awt.Toolkit;

public class Roles_buscarform extends JInternalFrame {
	private JTextField tbuscar;
	private JTable tabla_roles;
	private int filas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Roles_buscarform dialog = new Roles_buscarform();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public Roles_buscarform() {
		setFrameIcon(new ImageIcon(Asignarhorarios_buscarform.class.getResource("/icons/roles.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Rol");
		setBounds(100, 100, 502, 356);
		getContentPane().setLayout(null);
		filas=0;
		JLabel label = new JLabel("Nombre");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label.setBounds(10, 14, 55, 16);
		getContentPane().add(label);
		
		tbuscar = new JTextField();
		tbuscar.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tbuscar.setColumns(10);
		tbuscar.setBounds(75, 11, 290, 22);
		getContentPane().add(tbuscar);
		
		JButton btnBusacr = new JButton("Buscar");
		btnBusacr.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnBusacr.setContentAreaFilled(false);
		btnBusacr.setMnemonic(KeyEvent.VK_ENTER);
		btnBusacr.setIcon(new ImageIcon(Roles_buscarform.class.getResource("/icons/buscar.png")));
		btnBusacr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				metodoBuscar();
			}
		});
		btnBusacr.setBounds(375, 12, 101, 23);
		getContentPane().add(btnBusacr);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 41, 466, 266);
		getContentPane().add(scrollPane);
		
		tabla_roles = new JTable();
		scrollPane.setViewportView(tabla_roles);
		tabla_roles.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"codigo", "Nombre"
			}
		));
		tabla_roles.getColumnModel().getColumn(0).setPreferredWidth(0);
		tabla_roles.getColumnModel().getColumn(0).setMinWidth(0);
		tabla_roles.getColumnModel().getColumn(0).setMaxWidth(0);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(tabla_roles, popupMenu);
		
		JMenuItem mntmModificar = new JMenuItem("Modificar");
		mntmModificar.setIcon(new ImageIcon(Roles_buscarform.class.getResource("/icons/modificar_pq.png")));
		mntmModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(tabla_roles.getSelectedRow()>-1)
				{
					Roles_ingresarform roles = new Roles_ingresarform(tabla_roles.getValueAt(tabla_roles.getSelectedRow(), 0).toString());
					Principal.contenedor.add(roles);
					roles.setVisible(true);
				}
			}
		});
		popupMenu.add(mntmModificar);
		
		metodoBuscar();
		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		try
		{
			filas=0;
			DefaultTableModel mroles = (DefaultTableModel)tabla_roles.getModel();
			while(mroles.getRowCount()>0) mroles.removeRow(0);
			
			String query = "SELECT * FROM roles WHERE nombre LIKE ?";
			ArrayList<String> parametros = new ArrayList<String>();
			parametros.add("%"+tbuscar.getText()+"%");
			
			ResultSet resultado = new Roles_class().mostrar(query, parametros);
			while(resultado.next())
			{
				mroles.addRow(new Object[filas]);
				mroles.setValueAt(resultado.getString("codigorol"), filas, 0);
				mroles.setValueAt(resultado.getString("nombre"), filas, 1);
				filas++;
			}
			resultado.close();
			
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null, ex.toString());
		}
	
	}
}
